package tyk.Service;

import tyk.Dao.BaseDao;
import tyk.pojo.AccountPage;
import tyk.pojo.Cart;
import tyk.pojo.CartItems;
import tyk.pojo.OrderItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/***
 *@title OderItemService
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/18 17:53
 **/
public class OderItemService extends BaseDao {
    private String order_id;
    private Integer user_id;
    private Cart cart;

    public OderItemService() {
    }

    public OderItemService(String order_id, Integer user_id, Cart cart) {
        this.order_id = order_id;
        this.user_id = user_id;
        this.cart = cart;
    }
    public void CreateOrderItems(String order_id, Integer user_id, Cart cart) throws SQLException {
        Map<Integer, CartItems> map=cart.getMap();
        String sql="insert into t_order_items (name,count,price,total_price,order_id,status,user_id,merchan_id) values(?,?,?,?,?,?,?,?);";
        String sql2="update book set sales = sales + ? ,stock = stock - ? where id = ?;";
        for( Map.Entry<Integer, CartItems> it:map.entrySet()){
            CartItems cartItems=it.getValue();
            super.executeUpdate(sql,cartItems.getName(),cartItems.getCount(),cartItems.getPrice(),cartItems.getPriceTocal(),order_id,0,user_id,cartItems.getMerchant_id());
            super.executeUpdate(sql2,cartItems.getCount(),cartItems.getCount(),cartItems.getId());
        }
    }
    public List<OrderItem> SelectItemsByOrder_id(String order_id) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        List<OrderItem> list=new ArrayList<>();
        System.out.println("order_id="+order_id);
        String sql="select order_id,name,price,count,total_price,status,user_id,merchan_id from t_order_items where order_id =?;";
        list=super.executeQuery(OrderItem.class,sql,order_id);
        return list;
    }
    public List<OrderItem> SelectItemsByMerchan_id(Integer merchan_id, AccountPage accountPage) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        List<OrderItem> list=new ArrayList<>();
        String sql="select id,order_id,name,price,count,total_price,status,user_id,merchan_id from t_order_items where merchan_id =?;";
        list=super.executeQuery(OrderItem.class,sql,merchan_id);
        accountPage.setCount(list.size());
        accountPage.setPageTocal(accountPage.getCount()/12);
        if(accountPage.getCount()%12!=0) accountPage.setPageTocal(accountPage.getPageTocal()+1);
        if(accountPage.getPage()<1) accountPage.setPage(1);
        else if(accountPage.getPage()>accountPage.getPageTocal()) accountPage.setPage(accountPage.getPageTocal());
        String sql2="select id,order_id,name,price,count,total_price,status,user_id,merchan_id from t_order_items where merchan_id = ? limit ?,?;";
        list=super.executeQuery(OrderItem.class,sql2,merchan_id,(accountPage.getPage()-1)*12,12);
        System.out.println(list);
        return list;
    }
    public void SendOutGoods(Integer id,Integer value,Object ... params) throws SQLException {
        if(params.length==0){
            String sql="update t_order_items set status = ? where id = ?;";
            super.executeUpdate(sql,value,id);
        }else {
            String sql="update t_order_items set status = ? where order_id = ?;";
            super.executeUpdate(sql,value,params[0]);
            sql="update t_orders set status = ? where order_id = ?;";
            super.executeUpdate(sql,value,params[0]);
        }
    }
}
